#include "logger.h"
#include "converter.h"

void logger::write_to_file(const string &file_name, const string &message, bool line_feed, bool append) {
  const char *mode = (append == true) ? "ab" : "wb";

  FILE *f = fopen(file_name.c_str(), mode);
  if (f != 0) {
    fwrite(message.c_str(), sizeof(char), message.length(), f);
    if (line_feed == true)
      fwrite("\r\n", sizeof(char), strlen("\r\n"), f);

    fclose(f);
  } // if
} // logger::write_to_file

void logger::log(const string &file_name, const string &message, bool line_feed, bool append) {
  write_to_file(file_name, message, line_feed, append);
} // logger::log

void logger::log(const string &file_name, int number, bool line_feed, bool append) {
  string s;
  converter::to_string(number, s);
  write_to_file(file_name, s, line_feed, append);
} // logger::log

void logger::log(const string &file_name, char c, bool line_feed, bool append) {
  string s(1, c);
  write_to_file(file_name, s, line_feed, append);
} // logger::log
